<?php
namespace app\admin\controller;

class Access extends Base{
	public function __construct()
	{
		parent::__construct();
        $this->isLogin();
		$this->User = D('User');
        $active['fit']['rule']=1;
        $this->assign('active',$active);
	}
	
	public function rule()
	{
		$info = M('authRule')->select();
		$access = $this->get_tree($info);
		$this->assign('info',$access);
		
		//获取用户组
		$group = M('authGroup')->where('id='.$_GET['id'])->find();
		$this->assign('group_name',$group['title']);
		//判断选中状态
        $arr = $group['rules'];
		$this->assign('arr',$arr);

		
		return $this->view->fetch();
	}
	public function list_rule()
	{
		
		$info = M('term')->select();
		$access = $this->getTermTree(0,$info);
		$this->assign('info',$access);
		
		//获取用户组
		$group = M('authGroup')->where('id='.$_GET['id'])->find();
		$this->assign('groupname',$group['title']);
		
		$li_rule = $group['listrules'];
		$this->assign('li_rule',$li_rule);
		
		return $this->view->fetch();
	}
	
	public function save_list_rule()
	{
		if(IS_POST){
			$where['id']=I('post.id');
            if(isset($_POST['rules'])){
				sort($_POST['rules']);
				$data['listrules']  = implode( ',' , array_unique($_POST['rules']));
			}
			$save = M('authGroup')->where($where)->save($data);
			if($save){
				 return $this->success('保存成功', U('User/group')); 
			}else{
				 return $this->error('保存失败', U('User/group'));
			}
		}
	}

	
	public function rule_add()
	{
		$info = M('authRule')->select();
		$pid = $this->get_tree($info);
		$this->assign('pid',$pid);
		// 由上一级的path + 上一级的id 组成本级的path,pid是上一级的id;
		
		if(IS_POST){
			
			//取得上级的path_id
			$path_id = $_POST['path_id'];
			$string = explode('-',$path_id);
			$data['pid'] = $string[1];
			if($path_id == '0-0'){
				$data['path'] = "0";
			}else{
			$data['path'] = $path_id;
			}
			$data['module']=I('post.module');
			$data['name']=I('post.name');
			$data['title']=I('post.title');
			$data['type']=I('post.type');
			$data['status']=I('post.status');
			$rule_add = M('authRule')->add($data);
				if($rule_add){
						  return $this->success('新增成功', U('Access/rule_add')); 
					  }else{
						  return $this->error('新增失败', U('Access/rule_add')); 
					  }		
				}
			return $this->fetch();
	}
	
	public function rule_edit()
	{
		$where['id']=I('post.id');
		$info = M('authRule')->where($where)->find();
		if(IS_POST){
			$data['module']=I('post.module');
			$data['name']=I('post.name');
			$data['title']=I('post.title');
			$data['type']=I('post.type');
			$data['status']=I('post.status');
			$rule_edit = M('authRule')->where($where)->save($data);
				if($rule_edit){
					  return $this->success('修改成功', U('User/group')); 
				  }else{
					  return $this->error('修改失败', U('User/group')); 
				  }		
				}
		}
	

		public function save_auth()
		{
			
		  if(is_array($_POST['rules'])){
			if(isset($_POST['rules'])){
				sort($_POST['rules']);
				$_POST['rules']  = implode( ',' , array_unique($_POST['rules']));
			}
		  }else{
			$_POST['rules']=$_POST['rules'];
		  }
			$_POST['module'] =  'admin';
			$_POST['type']   =  1;
			$AuthGroup =  M('AuthGroup');
			$data = $AuthGroup->create();
			
			if ($data) {
				if(empty($data['id']) ) {
					$r = $AuthGroup->add();
				}else{
					$r = $AuthGroup->save();
				}
				if($r===false){
					 return $this->error('保存失败',U('User/group'));
				}else{
					 return $this->success('保存成功!',U('User/group'));
				}
			}else{
				 return $this->error('保存失败',U('User/group'));
			}
			
			

		}
	








	
	
}
